﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类House_LockedInfo。
    /// </summary>
    public class House_LockedInfo
    {
        private const string CONNECTSTRING_SEARCH = "Chenxin_House_Search";
        private const string CONNECTSTRING = "Chenxin_House";

        public House_LockedInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid LockedId)
        {
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from House_LockedInfo where LockedId=@LockedId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LockedId", DbType.Guid, LockedId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.House_LockedInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into House_LockedInfo(");
            strSql.Append("LockedId,HouseId,HouseCode,HouseAddress,LockedTime,LastTime,LockedStatus,CityName,CityId,AskerId,Asker,AskerDepartId,AskerDepart,Remarks,CompanyId,CompanyName,OperatorId,Operator,DepartId,Department,InputTime,UpdateTime)");

            strSql.Append(" values (");
            strSql.Append("@LockedId,@HouseId,@HouseCode,@HouseAddress,@LockedTime,@LastTime,@LockedStatus,@CityName,@CityId,@AskerId,@Asker,@AskerDepartId,@AskerDepart,@Remarks,@CompanyId,@CompanyName,@OperatorId,@Operator,@DepartId,@Department,@InputTime,@UpdateTime)");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LockedId", DbType.Guid, model.LockedId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseCode", DbType.AnsiString, model.HouseCode);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "LockedTime", DbType.DateTime, model.LockedTime);
            db.AddInParameter(dbCommand, "LastTime", DbType.DateTime, model.LastTime);
            db.AddInParameter(dbCommand, "LockedStatus", DbType.Int32, model.LockedStatus);
            db.AddInParameter(dbCommand, "AskerId", DbType.Guid, model.AskerId);
            db.AddInParameter(dbCommand, "Asker", DbType.AnsiString, model.Asker);
            db.AddInParameter(dbCommand, "AskerDepartId", DbType.Guid, model.AskerDepartId);
            db.AddInParameter(dbCommand, "AskerDepart", DbType.AnsiString, model.AskerDepart);
            db.AddInParameter(dbCommand, "Remarks", DbType.AnsiString, model.Remarks);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "OperatorId", DbType.Guid, model.OperatorId);
            db.AddInParameter(dbCommand, "Operator", DbType.AnsiString, model.Operator);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "Department", DbType.AnsiString, model.Department);
            db.AddInParameter(dbCommand, "InputTime", DbType.DateTime, model.InputTime);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.House_LockedInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update House_LockedInfo set ");
            strSql.Append("HouseId=@HouseId,");
            strSql.Append("HouseCode=@HouseCode,");
            strSql.Append("HouseAddress=@HouseAddress,");
            strSql.Append("LockedTime=@LockedTime,");
            strSql.Append("LastTime=@LastTime,");
            strSql.Append("LockedStatus=@LockedStatus,");
            strSql.Append("AskerId=@AskerId,");
            strSql.Append("Asker=@Asker,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("AskerDepartId=@AskerDepartId,");
            strSql.Append("AskerDepart=@AskerDepart,");
            strSql.Append("Remarks=@Remarks,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("OperatorId=@OperatorId,");
            strSql.Append("Operator=@Operator,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("Department=@Department,");
            strSql.Append("InputTime=@InputTime,");
            strSql.Append("UpdateTime=@UpdateTime");
            strSql.Append(" where LockedId=@LockedId ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LockedId", DbType.Guid, model.LockedId);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseCode", DbType.AnsiString, model.HouseCode);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "LockedTime", DbType.DateTime, model.LockedTime);
            db.AddInParameter(dbCommand, "LastTime", DbType.DateTime, model.LastTime);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "LockedStatus", DbType.Int32, model.LockedStatus);
            db.AddInParameter(dbCommand, "AskerId", DbType.Guid, model.AskerId);
            db.AddInParameter(dbCommand, "Asker", DbType.AnsiString, model.Asker);
            db.AddInParameter(dbCommand, "AskerDepartId", DbType.Guid, model.AskerDepartId);
            db.AddInParameter(dbCommand, "AskerDepart", DbType.AnsiString, model.AskerDepart);
            db.AddInParameter(dbCommand, "Remarks", DbType.AnsiString, model.Remarks);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "OperatorId", DbType.Guid, model.OperatorId);
            db.AddInParameter(dbCommand, "Operator", DbType.AnsiString, model.Operator);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "Department", DbType.AnsiString, model.Department);
            db.AddInParameter(dbCommand, "InputTime", DbType.DateTime, model.InputTime);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid LockedId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from House_LockedInfo ");
            strSql.Append(" where LockedId=@LockedId ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LockedId", DbType.Guid, LockedId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_LockedInfo GetModel(Guid LockedId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LockedId,HouseId,HouseCode,HouseAddress,LockedTime,LastTime,LockedStatus,CityName,CityId,AskerId,Asker,AskerDepartId,AskerDepart,Remarks,CompanyId,CompanyName,OperatorId,Operator,DepartId,Department,InputTime,UpdateTime from House_LockedInfo ");
            strSql.Append(" where LockedId=@LockedId ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LockedId", DbType.Guid, LockedId);
            Chenxin.Lib.House.Entity.House_LockedInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }

                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LockedId,HouseId,HouseCode,HouseAddress,LockedTime,LastTime,LockedStatus,CityName,CityId,AskerId,Asker,AskerDepartId,AskerDepart,Remarks,CompanyId,CompanyName,OperatorId,Operator,DepartId,Department,InputTime,UpdateTime ");
            strSql.Append(" FROM House_LockedInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "House_LockedInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.House_LockedInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LockedId,HouseId,HouseCode,HouseAddress,LockedTime,LastTime,LockedStatus,CityName,CityId,AskerId,Asker,AskerDepartId,AskerDepart,Remarks,CompanyId,CompanyName,OperatorId,Operator,DepartId,Department,InputTime,UpdateTime ");
            strSql.Append(" FROM House_LockedInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.House_LockedInfo> list = new List<Chenxin.Lib.House.Entity.House_LockedInfo>();
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }

                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_LockedInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.House_LockedInfo model = new Chenxin.Lib.House.Entity.House_LockedInfo();
            object ojb;
            ojb = dataReader["LockedId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LockedId = new Guid(ojb.ToString());
            }
            ojb = dataReader["HouseId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.HouseId = new Guid(ojb.ToString());
            }
            model.HouseCode = dataReader["HouseCode"].ToString();
            model.HouseAddress = dataReader["HouseAddress"].ToString();
            ojb = dataReader["LockedTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LockedTime = (DateTime)ojb;
            }
            ojb = dataReader["LastTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LastTime = (DateTime)ojb;
            }
            ojb = dataReader["LockedStatus"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LockedStatus = (int)ojb;
            }
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            ojb = dataReader["AskerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AskerId = new Guid(ojb.ToString());
            }
            model.Asker = dataReader["Asker"].ToString();
            ojb = dataReader["AskerDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AskerDepartId = new Guid(ojb.ToString());
            }
            model.AskerDepart = dataReader["AskerDepart"].ToString();
            model.Remarks = dataReader["Remarks"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["OperatorId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OperatorId = new Guid(ojb.ToString());
            }
            model.Operator = dataReader["Operator"].ToString();
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            model.Department = dataReader["Department"].ToString();
            ojb = dataReader["InputTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.InputTime = (DateTime)ojb;
            }
            ojb = dataReader["UpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdateTime = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

